#include "../common/config.h"
#include <boost/lexical_cast.hpp>
#include <Wt/WVBoxLayout>
#include <Wt/WGridLayout>
#include <Wt/WGroupBox>
#include <Wt/WLabel>
#include <Wt/WLength>
#include <Wt/WTimer>
#include "../common/logmanager.h"
#include "../httpproxy/httpproxy.h"
#include "../httpproxy/httpsession.h"
#include "Pad.h"
#include "StatusPage.h"

using namespace std;
using namespace log4cxx;
using namespace Wt;

StatusPage::StatusPage(WContainerWidget *parent)
	: WContainerWidget(parent), TimerPageBase(1000, parent)
{
	HTTPProxy &mainproxy = HTTPProxy::getInstance();
	WVBoxLayout *root_layout = new WVBoxLayout();


	WGroupBox *proxy_box = new WGroupBox("System Status", this);
	WGridLayout *proxy_layout = new WGridLayout();

	proxy_layout->addWidget(new WLabel("Proxy"), 0, 0, AlignLeft | AlignTop);
	if(mainproxy.isAlive())
		m_proxy_state = new WLabel("Running", this);
	else
		m_proxy_state = new WLabel("Stopped", this);
	proxy_layout->addWidget(m_proxy_state, 0, 1, AlignLeft | AlignTop);
	proxy_layout->addWidget(new WLabel("Sessions"), 1, 0, AlignLeft | AlignTop);
	m_session_counter = new WLabel(boost::lexical_cast<string>(HTTPSession::getSessionCounter()).c_str(),
								this);
	proxy_layout->addWidget(m_session_counter, 1, 1, AlignLeft | AlignTop);
	
	proxy_box->setLayout(proxy_layout);
	proxy_box->setMinimumSize(WLength(600), WLength());
	root_layout->addWidget(new VerticalPad(100), 0, AlignCenter | AlignTop);
	root_layout->addWidget(proxy_box, 1, AlignCenter | AlignTop);
	setLayout(root_layout);
}

void StatusPage::refresh()
{
	m_session_counter->setText(boost::lexical_cast<string>(HTTPSession::getSessionCounter()).c_str());
}

